package com.idcf.boathouse.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.idcf.boathouse.order.dao.BoatHouseProductDao;
import com.idcf.boathouse.order.entity.BoatHouseProduct;
import com.idcf.boathouse.order.entity.BoatHouseSet;
import com.idcf.boathouse.order.dao.BoatHouseSetDao;
import com.idcf.boathouse.order.entity.FoodPo;
import com.idcf.boathouse.order.entity.OrderPo;
import com.idcf.boathouse.order.service.BoatHouseSetService;
import com.idcf.boathouse.order.vo.FoodVo;
import com.idcf.boathouse.order.vo.OrderVo;
import com.idcf.boathouse.order.vo.ProductVo;
import com.idcf.boathouse.order.vo.SetVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * (BoatHouseSet)表服务实现类
 *
 * @author makejava
 * @since 2020-12-29 20:39:15
 */
@Service("boatHouseSetService")
public class BoatHouseSetServiceImpl implements BoatHouseSetService {
    @Resource
    private BoatHouseSetDao boatHouseSetDao;
    @Resource
    private BoatHouseProductDao boatHouseProductDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public BoatHouseSet queryById(Integer id) {
        return this.boatHouseSetDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<BoatHouseSet> queryAllByLimit(int offset, int limit) {
        return this.boatHouseSetDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param boatHouseSet 实例对象
     * @return 实例对象
     */
    @Override
    public BoatHouseSet insert(BoatHouseSet boatHouseSet) {
        this.boatHouseSetDao.insert(boatHouseSet);
        return boatHouseSet;
    }

    /**
     * 修改数据
     *
     * @param boatHouseSet 实例对象
     * @return 实例对象
     */
    @Override
    public BoatHouseSet update(BoatHouseSet boatHouseSet) {
        this.boatHouseSetDao.update(boatHouseSet);
        return this.queryById(boatHouseSet.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.boatHouseSetDao.deleteById(id) > 0;
    }

    @Override
    public List<SetVo> queryAll() {
        List<BoatHouseSet> boatHouseSets = boatHouseSetDao.queryAll();
        List<SetVo> setVos = new ArrayList<>();
        for (BoatHouseSet boatHouseSet : boatHouseSets) {
            SetVo setVo = new SetVo();
            BeanUtil.copyProperties(boatHouseSet, setVo);
            List<Integer> productIds = Arrays.stream(boatHouseSet.getProducts().split(",")).map(Integer::valueOf).collect(Collectors.toList());
            List<BoatHouseProduct> boatHouseProducts = boatHouseProductDao.queryByIds(productIds);
            List<ProductVo> productVos = new ArrayList<>();
            for (BoatHouseProduct boatHouseProduct : boatHouseProducts) {
                ProductVo productVo = new ProductVo();
                BeanUtil.copyProperties(boatHouseProduct, productVo);
                productVos.add(productVo);
            }
            setVo.setFoodList(productVos);
            setVos.add(setVo);
        }
        return setVos;
    }
}